
#include "TUIBase.h"

void TUI_onDispCH(unsigned char* buffer,int num,int CHItem);
void TUI_onRunState(unsigned char* buffer,int num,int useless);
void TUI_onDispHome(unsigned char* buffer,int num,int useless);
void TUI_onDispOpt(unsigned char* buffer,int num,int useless);
void TUI_onCHPres(unsigned char* buffer,int num,int CHItem);
void TUI_onCHState(unsigned char* buffer,int num,int useless);
void TUI_onCHParam(unsigned char* buffer,int num,int paramItem);
void TUI_onCHSave(unsigned char* buffer,int num,int useless);
void TUI_onCHPreview(unsigned char* buffer,int num,int useless);
void TUI_onCHCancel(unsigned char* buffer,int num,int useless);
void TUI_onCHPage(unsigned char* buffer,int num,int useless);
void TUI_onCHPrevBack(unsigned char* buffer,int num,int useless);
void TUI_onEnterPswd(unsigned char* buffer,int num,int useless);
void TUI_onSelectOpt(unsigned char* buffer,int num,int useless);
void TUI_onCfgPage(unsigned char* buffer,int num,int useless);
void TUI_onCfgSave(unsigned char* buffer,int num,int useless);
void TUI_onCfgGeneral(unsigned char* buffer,int num,int generalItem);
void TUI_onCfgLANType(unsigned char* buffer,int num,int useless);
void TUI_onCfgMethods(unsigned char* buffer,int num,int useless);
void TUI_onCfgLANAddr(unsigned char* buffer,int num,int useless);
void TUI_onCfgPswd(unsigned char* buffer,int num,int useless);

TUI_METHOD_ENTRY TUI_method_map[] =
{
  {TUI_BTN_CH1,               0,            TUI_onDispCH,     0},
  {TUI_BTN_CH2,               0,            TUI_onDispCH,     1},
  {TUI_BTN_CH3,               0,            TUI_onDispCH,     2},
  {TUI_BTN_RUN,               0,            TUI_onRunState,   0},
  {TUI_BTN_HOME,              0,            TUI_onDispHome,   0},
  {TUI_BTN_OPT,               0,            TUI_onDispOpt,    0},
  {TUI_BTN_CH1_SET,           0,            TUI_onCHPres,     0},
  {TUI_BTN_CH2_SET,           0,            TUI_onCHPres,     1},
  {TUI_BTN_CH3_SET,           0,            TUI_onCHPres,     2},
  {TUI_BTN_CH4_SET,           0,            TUI_onCHPres,     3},
  {TUI_BTN_CH5_SET,           0,            TUI_onCHPres,     4},
  {TUI_BTN_CH6_SET,           0,            TUI_onCHPres,     5},
  {TUI_BTN_CH_ON,             0,            TUI_onCHState,    0},
  {TUI_BTN_CH_SET_INIT,       0,            TUI_onCHParam,    0},
  {TUI_BTN_CH_SET_INITT,      0,            TUI_onCHParam,    1},
  {TUI_BTN_CH_SET_RATE1,      0,            TUI_onCHParam,    2},
  {TUI_BTN_CH_SET_PRE1,       0,            TUI_onCHParam,    3},
  {TUI_BTN_CH_SET_TIME1,      0,            TUI_onCHParam,    4},
  {TUI_BTN_CH_SET_RATE2,      0,            TUI_onCHParam,    5},
  {TUI_BTN_CH_SET_PRE2,       0,            TUI_onCHParam,    6},
  {TUI_BTN_CH_SET_TIME2,      0,            TUI_onCHParam,    7},
  {TUI_BTN_CH_SET_RATE3,      0,            TUI_onCHParam,    8},
  {TUI_BTN_CH_SET_PRE3,       0,            TUI_onCHParam,    9},
  {TUI_BTN_CH_SET_TIME3,      0,            TUI_onCHParam,    10},
  {TUI_BTN_CH_SET_RATE4,      0,            TUI_onCHParam,    11},
  {TUI_BTN_CH_SET_PRE4,       0,            TUI_onCHParam,    12},
  {TUI_BTN_CH_SET_TIME4,      0,            TUI_onCHParam,    13},
  {TUI_BTN_CH_SET_TOTALT,     0,            TUI_onCHParam,    14},
  {TUI_BTN_CH_SAVE,           0,            TUI_onCHSave,     0},
  {TUI_BTN_CH_PREVIEW,        0,            TUI_onCHPreview,  0},
  {TUI_BTN_CH_CANCEL,         0,            TUI_onCHCancel,   0},
  {TUI_BTN_CH_PAGENUM,        0,            TUI_onCHPage,     0},
  {TUI_BTN_CH_PREVIEW_BACK,   0,            TUI_onCHPrevBack, 0},
  {TUI_BTN_LOCK_PASSWORD,     0,            TUI_onEnterPswd,  0},
  {TUI_BTN_OPT_SELECT,        0,            TUI_onSelectOpt,  0},
  {TUI_CFG_PAGE_UPDOWN,       0,            TUI_onCfgPage,    0},
  {TUI_CFG_SAVE,              0,            TUI_onCfgSave,    0},
  {TUI_CFG_GENERAL_UNIT,      0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_UNIT},
  {TUI_CFG_GENERAL_AUTOD,     0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_AUTOD},
  {TUI_CFG_GENERAL_RUN,       0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_RUN},
  {TUI_CFG_GENERAL_AFTERRUN,  0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_AFTERRUN},
  {TUI_CFG_GENERAL_RECVALARM, 0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_RECVALARM},
  {TUI_CFG_GENERAL_AFTERERROR,0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_AFTERERROR},
  {TUI_CFG_GENERAL_LOCKPSWD,  0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_LOCKPSWD},
  {TUI_CFG_GENERAL_HOUR,      0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_HOUR},
  {TUI_CFG_GENERAL_MINUTE,    0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_MINUTE},
  {TUI_CFG_GENERAL_YEAR,      0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_YEAR},
  {TUI_CFG_GENERAL_MONTH,     0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_MONTH},
  {TUI_CFG_GENERAL_DAY,       0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_DAY},
  {TUI_CFG_GENERAL_WAITT,     0,            TUI_onCfgGeneral, TUI_CFG_GENERAL_WAITT},
  {TUI_CFG_LAN_SET,           0,            TUI_onCfgLANType, 0},
  {TUI_CFG_LOADM_ADDR_START,  0x00F0,       TUI_onCfgMethods, 0},
  {TUI_CFG_LAN_IP_VALUE,      0x0060,       TUI_onCfgLANAddr, 0},
  {TUI_CFG_ACCOUNT_PSWD1,     0,            TUI_onCfgPswd,    TUI_CFG_ACCOUNT_PSWD1},
  {TUI_CFG_ACCOUNT_PSWD2,     0,            TUI_onCfgPswd,    TUI_CFG_ACCOUNT_PSWD2},
};
const int size_TUI_method_map = sizeof(TUI_method_map) / sizeof(TUI_METHOD_ENTRY);


void TUI_onDispCH(unsigned char* buffer,int num,int CHItem)
{
  if(m_TUIStatus.cfgStep == TUI_CFG_LOADM)
  {
    TUI_methodPreview(m_TUIStatus.MethodPreview,CHItem);
  }
  else
  {
    m_TUIStatusBackup = m_TUIStatus;
    TUI_dispCH(CHItem);
  }
}

void TUI_onRunState(unsigned char* buffer,int num,int useless)
{
  TUI_setRun((~m_TUIStatus.runState) & 0x01);
}

void TUI_onDispHome(unsigned char* buffer,int num,int useless)
{
  m_TUIStatus.cfgStep = TUI_CFG_NONE;
  TUI_dispHome();
}

void TUI_onDispOpt(unsigned char* buffer,int num,int useless)
{
  TUI_configStep(TUI_CFG_GENERAL);
}

void TUI_onCHPres(unsigned char* buffer,int num,int CHItem)
{
  if(num < 11)
    return;
  long m_val = (buffer[7]<<24)+(buffer[8]<<16) + (buffer[9]<<8)+buffer[10];
  m_val = m_val * unitScale[m_UserConfig.PresUnit];
  m_TUIStatus.CHs[CHItem].nowPres.value_set = m_val;
  TUI_setCH_Pressure(CHItem,m_val);
}
void TUI_onCHState(unsigned char* buffer,int num,int useless)
{
  m_TUIStatus.CHs[m_TUIStatus.CHItem].setON = (~m_TUIStatus.CHs[m_TUIStatus.CHItem].isON)&0x01;
}
void TUI_onCHParam(unsigned char* buffer,int num,int paramItem)
{
  long m_val = 0;
  if(num < 11)
    return;
  m_val = (buffer[7]<<24)+(buffer[8]<<16) + (buffer[9]<<8)+buffer[10];
  m_val = m_val * unitScale[m_UserConfig.PresUnit];
  m_TUIStatus.CHs[m_TUIStatus.CHItem].Value[paramItem].value_set = m_val;
}
void TUI_onCHSave(unsigned char* buffer,int num,int useless)
{
  m_TUIConfig.MethodUsing.CHs[m_TUIStatus.CHItem] = m_TUIStatus.CHs[m_TUIStatus.CHItem];
  TUI_configSave();
  TUI_setCH_config(m_TUIStatus.CHItem);
}
void TUI_onCHPreview(unsigned char* buffer,int num,int useless)
{
  TUI_dispPreview(m_TUIStatus.CHs[m_TUIStatus.CHItem]);
}
void TUI_onCHCancel(unsigned char* buffer,int num,int useless)
{
  if(num < 9)
    return;
  if(buffer[8]==1)//Yes
  {
    m_TUIStatus = m_TUIStatusBackup;
    TUI_dispPage(PAGE_HOME);
  }
}
void TUI_onCHPage(unsigned char* buffer,int num,int useless)
{
  if(num < 9)
    return;
  m_TUIStatus.nowPage = buffer[8];
}
void TUI_onCHPrevBack(unsigned char* buffer,int num,int useless)
{
  if( m_TUIStatus.cfgStep == TUI_CFG_LOADM)
    TUI_back2LoadM();
  else
    TUI_dispCHPage(m_TUIStatus.nowPage);
}
void TUI_onEnterPswd(unsigned char* buffer,int num,int useless)
{
  if(num <11)
    return;
  long m_val = (buffer[7]<<24)+(buffer[8]<<16) + (buffer[9]<<8)+buffer[10];
  if(m_UserConfig.Password == m_val)
    TUI_dispPage(PAGE_HOME);
}
void TUI_onSelectOpt(unsigned char* buffer,int num,int useless)
{
  if(num <9)
    return;
  TUI_configStep(buffer[8]);
}
void TUI_onCfgPage(unsigned char* buffer,int num,int useless)
{
  if(num <9)
    return;
  m_TUIStatus.nowPage = buffer[8];
}
void TUI_onCfgSave(unsigned char* buffer,int num,int useless)
{
  TUI_configSave();
}
void TUI_onCfgGeneral(unsigned char* buffer,int num,int generalItem)
{
  switch(generalItem)
  {
  case TUI_CFG_GENERAL_UNIT:
  case TUI_CFG_GENERAL_AUTOD:
  case TUI_CFG_GENERAL_RUN :
  case TUI_CFG_GENERAL_AFTERRUN :
  case TUI_CFG_GENERAL_RECVALARM :
  case TUI_CFG_GENERAL_AFTERERROR: 
  case TUI_CFG_GENERAL_LOCKPSWD : 
    TUI_changeGeneral(generalItem,0);
    break;
  case TUI_CFG_GENERAL_HOUR : 
  case TUI_CFG_GENERAL_MINUTE: 
  case TUI_CFG_GENERAL_YEAR : 
  case TUI_CFG_GENERAL_MONTH:
  case TUI_CFG_GENERAL_DAY:
  case TUI_CFG_GENERAL_WAITT :
    {
      if(num < 11)
        break;
      long m_val = (buffer[7]<<24)+(buffer[8]<<16) + (buffer[9]<<8)+buffer[10];
      TUI_changeGeneral(generalItem,m_val);
      break;
    }
  default:
    break;
  }
}
void TUI_onCfgLANType(unsigned char* buffer,int num,int useless)
{
  if(num < 9)
    return;
  m_TUIConfig.LanType = buffer[8];
  TUI_updateConfig(0);
}
void TUI_onCfgMethods(unsigned char* buffer,int num,int useless)
{
  unsigned short Addr = 0;
  if(num<9)
    return;
  if(buffer[8] != 1) //cancel
    return;
  Addr = (buffer[4] << 8) + buffer[5];
  TUI_configLoadM(Addr);
}
void TUI_onCfgLANAddr(unsigned char* buffer,int num,int useless)
{
  unsigned short Addr = 0;
  if(num<11)
    return;
  Addr = (buffer[4] << 8) + buffer[5];
  TUI_configLANAddr(Addr,buffer[10]);
}
void TUI_onCfgPswd(unsigned char* buffer,int num,int Addr)
{
  if(num<11)
    return;
  Addr = (buffer[4] << 8) + buffer[5];
  TUI_configLANAddr(Addr,buffer[10]);
}
